package com.che.libcommon.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.che.common.libcommon.R;
import com.che.libcommon.utils.ToastUtils;

import io.reactivex.disposables.CompositeDisposable;

public abstract class BaseFragment extends Fragment implements ICloseWatcher {
    protected FragmentCallBack callBack;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof FragmentCallBack) {
            callBack = (FragmentCallBack) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        callBack = null;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (callBack != null) callBack.configTitle(getFragmentTitle());
    }

    public void showToast(String text) {
        ToastUtils.showText(text);
    }


    public void finish() {
        final FragmentActivity activity = getActivity();
        if (activity != null && !activity.isFinishing()) {
            activity.finish();
        }
    }

    public boolean isFinish() {
        final FragmentActivity activity = getActivity();
        return activity == null || activity.isFinishing();
    }

    public String getFragmentTitle() {
        return getString(R.string.app_name);
    }

    public interface FragmentCallBack {
        void configTitle(String title);
    }

    public String getPageName(){
        return getClass().getSimpleName();
    }

    public boolean onBackPress(){
        return false;
    }

    public @Nullable View getTitleRightView(@NonNull Context context){
        return null;
    }

    @Override
    public void finishView(){
        finish();
    }
}
